package yogiro.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import yogiro.service.CommentService;
import yogiro.to.CommentTO;

public class CommentController extends MultiActionController{
	private CommentService service;
	public CommentController(CommentService service){
		this.service = service;
	}
	
	//삽입하는 메소드
	public ModelAndView insertComment(HttpServletRequest request, HttpServletResponse response){
		String grade = request.getParameter("grade");
		String content = request.getParameter("content");
		String id = request.getParameter("id");
		int tourNo = Integer.parseInt(request.getParameter("tourNo"));
		ModelAndView mv = null;
		try {
			CommentTO cto = service.insertComment(grade, content, id,tourNo);
			cto.setCommentContent(service.replace(cto.getCommentContent()));
			
			mv = new ModelAndView("jsonView","result",cto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	//삭제하는 메소드
	public ModelAndView deleteComment(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		ArrayList list = null;
		int commentNo = Integer.parseInt(request.getParameter("commentNo"));
		int tourNo = Integer.parseInt(request.getParameter("tourNo"));
		try {
			service.deleteComment(commentNo, tourNo);
			
			list = service.selectAllComment(tourNo);
			
			mv = new ModelAndView("jsonView","list",list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	public ModelAndView seleteAllComment(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = null;
		ArrayList list = null;
		int tourNo = Integer.parseInt(request.getParameter("tourNo"));
		try {
			
			list = service.selectAllComment(tourNo);
			
			mv = new ModelAndView("jsonView","list",list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mv;
	}
	
}
